/*************************************************************************
	> File Name: access.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: 六  3/29 21:07:16 2014
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
    if(argc<2)
    {
        printf("run by: ./a.out filepath\n");
        exit(0);
    }
    if(access(argv[1],F_OK)!=-1)
    {
        if(access(argv[1],R_OK)==-1 && access(argv[1],W_OK)==-1 && access(argv[1],X_OK)==-1)
        {
            printf("You DO NOT have any permission on this file\n");
            exit(0);
        }
        else if(access(argv[1],R_OK)!=-1)
        {
            printf("you can read this file\n");
            if(access(argv[1],W_OK)!=-1)
                printf("you can write this file\n");
            else
                printf("you don't have the permission to write this file\n");
        }
        else if(access(argv[1],W_OK)!=-1)
        {
            printf("you can write this file\n");
            if(access(argv[1],R_OK)!=-1)
                printf("you can read this file\n");
            else
                printf("you don't have the permission to read this file\n");
        }
        if(access(argv[1],X_OK)!=-1)
            printf("you can execute this file\n");
        else if(access(argv[1],X_OK)==-1) 
                printf("this file cannot be excuted\n");
    }
    else
    {
        printf("file doesn't exist!\n");
        exit(0);
    }
}
